// stdio.c
#ifndef __STDIO_C
#define __STDIO_C
#define LCD (*0x800c)
#define RS  0x0100
#define EN  0x0200
#define DELAY 0;0;0;0;0;0;0;0;0;0;0;0

int write_byte(int byte);

int write_byte(int byte)
{
	LCD = byte;
	0;
	LCD |= EN;
	DELAY;
	LCD &= ~EN;
	return 0;
}

int initio(void)
{
	delay10ms(2);
	write_byte(0x0030);
	delay10ms(1);
	write_byte(0x0030);
	delay10ms(1);
	write_byte(0x0030);
	delay10ms(1);
	write_byte(0x0038);
	delay10ms(1);
	write_byte(0x000C);
	delay10ms(1);
	write_byte(0x0001);
	delay10ms(1);
	write_byte(0x0006);
	delay10ms(1);
	write_byte(0x0080);
	delay10ms(1);
	return 0;
}

int printf(const char * s)
{
	int n = 0;
	while(*s)
	{
		/*if(*s == '%')
		{
			++i;
			if(*++s == 'd')
			{
				n += printd(*(int *)(&s-(i*2)));
			}
			else if(*s == 'x')
			{
				n += printx(*(int *)(&s-(i*2)));
			}
			else if(*s == 's')
			{
				n += prints((const char *)*(int *)(&s-(i*2)));
			}
		}*/
		printc(*s);
		++s;
		++n;
	}
	return n;
}

int printc(char c)
{
	int x;
	x = (int)c;
	x |= RS;
	write_byte(x);
	delay10ms(1);
	return 0;
}


int printd(int d)
{
	int j;
	int i = 10000;
	if(!d)
	{
		printc('0');
		return 1;
	}
	else
	{
		while(!(d / i))
		{
			i /= 10;
		}
		i *= 10;
		while(i > 1)
		{
			d = d % i;
			i /= 10;
			printc('0'+(char)(d/i));
			++j;
		}
		return j;
	}
}

int printx(int x)
{
	int j;
	int i = 0xFFFF;
	if(!x)
	{
		printc('0');
		return 1;
	}
	else
	{
		while(!(x / i))
		{
			i /= 16;
		}
		i *= 16;
		while(i > 1)
		{
			x = x % i;
			i /= 16;
			printc('0'+(char)(x/i));
			++j;
		}
		return j;
	}
}

int prints(char * s)
{
	while(*s)
	{
		printc(*s++);
	}
}

#endif